package com.example.demo.controller;

import com.example.demo.Service.BorrowService;
import com.example.demo.Service.LibrarianService;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.Borrow;
import com.example.demo.model.Librarian;
import com.example.demo.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/circulate")
@RestController
public class BorrowController {

    @Autowired
    LibrarianService librarianService;

    @Autowired
    BorrowService borrowService;

    @PostMapping("borrow")
    public JsonData borrowBook(@RequestBody Borrow borrow){
        Integer result = borrowService.borrowBook(borrow);
        if(result == -1){
            return JsonData.buildError("无此本书");
        } else if(result == 0){
            return JsonData.buildError("此本书已被借走");
        }else {
            return JsonData.buildSuccess(result);
        }
    }

    @PostMapping("return")
    public JsonData returnBook(@RequestBody Borrow borrow){
        Integer result = borrowService.returnBook(borrow);
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("还书失败");
        }
    }

    @GetMapping("borrow/list")
    public JsonData getBorrowList(){
        List<Borrow> result = borrowService.getBorrowList();
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("获取借阅信息失败");
        }
    }

    @GetMapping("borrow/list/{reader_id}")
    public JsonData getBorrowList(@PathVariable("reader_id") Integer reader_id){
        System.out.println(reader_id);
        List<Borrow> result = borrowService.getBorrowById(reader_id);
        if(result != null){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("获取借阅信息失败");
        }
    }

    @PostMapping("borrow/modify")
    public JsonData modifyBorrow(@RequestBody Borrow borrow){
        Integer result = borrowService.modifyBorrow(borrow);
        if(result != 0){
            return JsonData.buildSuccess(result);
        }else {
            return JsonData.buildError("更新失败");
        }
    }


}
